import pyaudio
import numpy as np
from scipy.io import wavfile
import sounddevice as sd
import speech_recognition as sr

class AudioInput:
    def __init__(self, sample_rate=16000, chunk_size=1024):
        self.sample_rate = sample_rate
        self.chunk_size = chunk_size
        self.recognizer = sr.Recognizer()
        self.microphone = sr.Microphone()
        
    def listen(self):
        """监听麦克风输入并返回音频数据"""
        print("请说话...")
        with self.microphone as source:
            audio = self.recognizer.listen(source)
        return audio.get_raw_data()
    
    def recognize(self, audio_data):
        """将音频数据转换为文本"""
        audio = sr.AudioData(audio_data, self.sample_rate, 2)
        try:
            text = self.recognizer.recognize_google(audio, language='zh-CN')
            return text
        except sr.UnknownValueError:
            return "无法识别语音"
        except sr.RequestError:
            return "语音识别服务不可用"